package com.c6906.vdam.dps.service.impl;

import cn.hutool.core.date.DateUtil;
import com.c6906.vdam.dps.common.DateUtils;
import com.c6906.vdam.dps.dao.TerminalDataDAO;
import com.c6906.vdam.dps.entity.TerminalBusinessInfo;
import com.c6906.vdam.dps.entity.TerminalDMData;
import com.c6906.vdam.dps.entity.TerminalData;
import com.c6906.vdam.dps.service.TerminalDataService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author : TAOHUIQI
 * @Date: 2024/6/16 21:09
 * @Description:
 */
@Service
public class TerminalDataServiceImpl implements TerminalDataService {

    @Resource
    TerminalDataDAO terminalDataDAO;

    public int batchInsertDataToDM(List<TerminalDMData> dataList) {
        return terminalDataDAO.batchInsert(dataList);
    }

    public int batchInsertDataMQToDM(List<TerminalData> dataList) {
        if (!CollectionUtils.isEmpty(dataList)) {
            List<TerminalDMData> dmDataList = generateTerminalDataMQToDM(dataList);
            return terminalDataDAO.batchInsert(dmDataList);
        } else {
            return 0;
        }
    }

    private List<TerminalDMData> generateTerminalDataMQToDM(List<TerminalData> dataList) {
        List<TerminalDMData> dmDataList = new ArrayList<>();
        for (TerminalData terminalData : dataList) {
            TerminalDMData terminalDMData = new TerminalDMData();
            terminalDMData.setTerminalSn(terminalData.getTerminalSn());
            terminalDMData.setTerminalSIM(terminalData.getTerminalSIM());
            terminalDMData.setIsFixed(DateUtils.strToInteger(terminalData.getIsFixed()));
            terminalDMData.setLongitude(DateUtils.strToBigDecimal(terminalData.getLongitude()));
            terminalDMData.setLatitude(DateUtils.strToBigDecimal(terminalData.getLatitude()));
            terminalDMData.setElevation(DateUtils.strToBigDecimal(terminalData.getElevation()));
            terminalDMData.setDirection(DateUtils.strToBigDecimal(terminalData.getDirection()));
            terminalDMData.setTerminalTime(terminalData.getTerminalTime());
            terminalDMData.setTerminalMiles(DateUtils.strToBigDecimal(terminalData.getTerminalMiles()));
            terminalDMData.setTerminalMilesAday(DateUtils.strToBigDecimal(terminalData.getTerminalMilesAday()));
            terminalDMData.setSpeed(DateUtils.strToBigDecimal(terminalData.getSpeed()));
            terminalDMData.setTerminalOverSpeed(DateUtils.strToInt(terminalData.getTerminalOverSpeed()));
            terminalDMData.setTerminalOffLine(DateUtils.strToInt(terminalData.getTerminalOffLine()));
            terminalDMData.setTerminalTiredDrive(DateUtils.strToInt(terminalData.getTerminalTiredDrive()));
            terminalDMData.setTerminalGnssError(DateUtils.strToInt(terminalData.getTerminalGnssError()));
            terminalDMData.setTerminalAntOpen(DateUtils.strToInt(terminalData.getTerminalAntOpen()));
            terminalDMData.setTerminalAntShort(DateUtils.strToInt(terminalData.getTerminalAntShort()));
            terminalDMData.setTerminalUnderVol(DateUtils.strToInt(terminalData.getTerminalUnderVol()));
            terminalDMData.setTerminalNoVol(DateUtils.strToInt(terminalData.getTerminalNoVol()));
            terminalDMData.setTerminalAccStatus(DateUtils.strToInt(terminalData.getTerminalAccStatus()));
            terminalDMData.setTerminalNetRssi(DateUtils.strToInt(terminalData.getTerminalNetRssi()));
            terminalDMData.setFixBeidou(DateUtils.strToInt(terminalData.getFixBeidou()));
            terminalDMData.setFixGps(DateUtils.strToInt(terminalData.getFixGps()));
            terminalDMData.setFixGlonass(DateUtils.strToInt(terminalData.getFixGlonass()));
            terminalDMData.setFixGalileo(DateUtils.strToInt(terminalData.getFixGalileo()));
            terminalDMData.setTerminalGnssCnt(DateUtils.strToInt(terminalData.getTerminalGnssCnt()));
            terminalDMData.setRegionCode(terminalData.getRegionCode());
            terminalDMData.setVehiclesCode(terminalData.getVehiclesCode());
            terminalDMData.setVehPlateNum(terminalData.getVehPlateNum());
            terminalDMData.setOwnUnitId(terminalData.getOwnUnitId());
            terminalDMData.setUseUnitId(Long.valueOf(terminalData.getUseUnitId()));
            terminalDMData.setVehUseType(terminalData.getVehUseType());
            terminalDMData.setVehOwnType(terminalData.getVehOwnType());
//            terminalDMData.setOwnUnitName(terminalData.getOwnUnitName());
//            terminalDMData.setUseUnitName(terminalData.getUseUnitName());
            terminalDMData.setVehPropId(terminalData.getVehPropId());
            terminalDMData.setDataCreate(DateUtils.dateToStr(new Date()));
            terminalDMData.setPartitionField(DateUtils.StrDateToIntDate(terminalData.getTerminalTime()));
            dmDataList.add(terminalDMData);
        }
        return dmDataList;
    }

    private TerminalData generateTerminalData(TerminalData terminalData, TerminalBusinessInfo terminalBusinessInfo) {
        terminalData.setRegionCode(terminalBusinessInfo.getRegionCode());
        terminalData.setVehiclesCode(terminalBusinessInfo.getVehiclesCode());
        terminalData.setVehPlateNum(terminalBusinessInfo.getVehPlateNum());
        terminalData.setOwnUnitId(terminalBusinessInfo.getOwnUnitId());
        terminalData.setUseUnitId(terminalBusinessInfo.getUseUnitId());
        terminalData.setVehUseType(terminalBusinessInfo.getVehUseType());
        terminalData.setVehOwnType(terminalBusinessInfo.getVehOwnType());
//        terminalData.setOwnUnitName(terminalBusinessInfo.getOwnUnitName());
//        terminalData.setUseUnitName(terminalBusinessInfo.getUseUnitName());
        terminalData.setVehPropId(terminalBusinessInfo.getVehPropId());
        return terminalData;
    }

}
